using System;
using System.Xml;
using System.Collections;
using System.Runtime.InteropServices;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Property
{
	//*********************************************************************
	//
	// XMLPropertyValues Class
	//
	// The XMLPropertyValues class represents a collection base of XMLPropertyValue
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLPropertyValues Has no fields
	//
	//*********************************************************************
	public class XMLPropertyValues : bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE	= "Values";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLPropertyValues()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLPropertyValues(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		override public void Init()
		{
			this.RemoveAll();
		}

		/** INSTANCE METHODS **/

		/** Parsering method for filling the values **/
		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLPropertyValue.MAIN_NODE:
						this.Add ( new XMLPropertyValue(node));
						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLPropertyValues.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** Method for return a value from collection **/
		public XMLPropertyValue Get(string sValue)
		{
			XMLPropertyValue oneValue;

			for ( int i=0; i<this.List.Count; i++)
			{
				oneValue = (XMLPropertyValue)this.List[i];
				if ( oneValue.sValue == sValue )
				{
					return oneValue;
				}
			}

			throw new Exception.PropertyValueNotFound(sValue + " not found.");
		}


		/** Method for checking a value in collection **/
		public bool AlmostIn(XMLPropertyValues vValues)
		{
			XMLPropertyValue leftValue;
			XMLPropertyValue rightValue;

			for ( int i=0; i<this.List.Count; i++)
			{
				leftValue = (XMLPropertyValue)this.List[i];

				try
				{
					rightValue = vValues.Get(leftValue.sValue);
				}
				catch ( Exception.PropertyValueNotFound)
				{
					return false;
				}
			}
			
			return true;
		}


		/** method to remove a value from collection by its value**/
		public bool RemoveByValue(string sValue)
		{
			XMLPropertyValue PropertyValue;

			for ( int i=0; i<this.List.Count; i++)
			{
				PropertyValue = (XMLPropertyValue)this.List[i];

				if (PropertyValue.sValue == sValue)
				{
					this.List.Remove(PropertyValue);
					return true;
				}
			}
			
			return false;
		}

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Value
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Value:
					InnerList.Sort(new ValueComparer());
					break;
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class ValueComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLPropertyValue first = (XMLPropertyValue) x;
				XMLPropertyValue second = (XMLPropertyValue) y;
				return first.Value.CompareTo(second.Value);
			}
		}
	}
}